<?php

namespace addons\mall\controller;

use addons\mall\library\Service;
use app\common\model\mall\oper\Message;
use app\common\model\mall\oper\Notification;

/**
 * 前台用户支付密码
 *
 * @icon fa fa-circle-o
 */
class User extends Base
{
    /**
     * Extra模型对象
     * @var \app\common\model\mall\user\Extra
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $id = $this->request->param('id', $this->auth->id);
        $user = \addons\mall\model\User::get($id);
        if (!$user) {
            $this->error("未找到指定用户");
        }
        $user->combine();

        $this->user = $user;

        $this->view->assign('__user__', $user);

    }

    /**
     * 分发
     * @return string
     * @throws \think\Exception
     */
    public function dispatch()
    {
        $dispatch = $this->request->param('dispatch', '');
        //是否为分发路由
        if ($dispatch && method_exists($this, $dispatch)) {
            $this->request->action($dispatch);
            $this->view->assign('action', $dispatch);
            return $this->$dispatch();
        }
        return $this->view->fetch();
    }

    /**
     * 我的通知
     */
    public function notification()
    {
        if ($this->auth->id != $this->user->id) {
            $this->error("无法进行越权操作");
        }
        $act = $this->request->request('act');
        if ($act) {
            if ($act == 'marktopall') {
                if ($this->user->notifications || $this->user->messages) {
                    \addons\ask\model\User::where('user_id', $this->user->id)->update(['notifications' => 0, 'messages' => 0]);
                    $this->auth->getUser()->mall->notifications = 0;
                    $this->auth->getUser()->mall->messages = 0;
                }
            } elseif ($act == 'marktop') {
                if ($this->user->notifications) {
                    \addons\ask\model\User::where('user_id', $this->user->id)->update(['notifications' => 0]);
                    $this->auth->getUser()->mall->notifications = 0;
                }
            } elseif ($act == 'markall') {
                Notification::where('to_user_id', $this->auth->id)->where('isread', 0)->update(['isread' => 1]);
                $this->user->data(['notifications' => 0])->save();
                $this->success('标记成功');
            } else {
                $notification_id = $this->request->request('notification_id');
                $notification = Notification::where('id', $notification_id)->where('to_user_id', $this->auth->id)->find();
                if (!$notification) {
                    $this->error('未找到指定信息');
                }
                if ($act == 'del') {
                    $notification->delete();
                    $this->user->setDec('notifications');
                    $this->success('删除成功');
                } elseif ($act == 'mark') {
                    $notification->isread = 1;
                    $notification->save();
                    $this->user->setDec('notifications');
                    $this->success('标记成功');
                }
            }
        }
        $notificationList = Notification::with('from')
            ->where('to_user_id', $this->user->id)
            ->order('id desc')
            ->paginate();
        $this->view->assign('title', '我的通知');
        $this->view->assign('notificationList', $notificationList);

        if ($this->request->isAjax()) {
            return $this->view->fetch("user/ajax/get_notification_list");
        }
        return $this->view->fetch();
    }

    /**
     * 我的私信
     */
    public function message()
    {
        if ($this->auth->id != $this->user->id) {
            $this->error("无法进行越权操作");
        }
        $act = $this->request->request('act');
        if ($act) {
            if ($act == 'marktop') {
                if ($this->user->messages) {
                    \addons\ask\model\User::where('user_id', $this->user->id)->update(['messages' => 0]);
                    $this->auth->getUser()->ask->messages = 0;
                }
            } elseif ($act == 'markall') {
                Message::where('to_user_id', $this->auth->id)->where('isread', 0)->update(['isread' => 1]);
                if ($this->user->messages) {
                    \addons\ask\model\User::where('user_id', $this->user->id)->update(['messages' => 0]);
                    $this->auth->getUser()->ask->messages = 0;
                }
                $this->success('标记成功');
            } else {
                $message_id = $this->request->request('message_id');
                $message = Message::where('id', $message_id)->where('to_user_id', $this->auth->id)->find();
                if (!$message) {
                    $this->error('未找到指定信息');
                }
                if ($act == 'del') {
                    $message->istodeleted = 1;
                    $message->save();
                    //如果双边都删除
                    if ($message->isfromdeleted && $message->istodeleted) {
                        $message->delete();
                    }
                    $this->user->setDec("messages");
                    $this->success('删除成功');
                } elseif ($act == 'mark') {
                    $message->isread = 1;
                    $message->save();
                    $this->user->setDec("messages");
                    $this->success('标记成功');
                }
            }
        }
        $messageList = Message::with('from')
            ->where('to_user_id', $this->user->id)
            ->where('istodeleted', 0)
            ->order('id desc')
            ->paginate();
        $this->view->assign('title', '我的私信');
        $this->view->assign('messageList', $messageList);
        if ($this->request->isAjax()) {
            return $this->view->fetch("user/ajax/get_message_list");
        }
        return $this->view->fetch();
    }

    /**
     * 修改用户支付密码
     */
    public function editPayPass(){
        //获取用户信息
        if (!$this->user) $this->error(__('No Results were found'));

        $data = array();
        $newPass = input("post.newPass");
        if (!$newPass) {
            $this->error('支付密码不能为空');
        }
        if ($this->user['paypwd']) {//有旧密码则修改
            $oldPass = input("post.oldPass");
            if ($this->user['paypwd'] == md5($oldPass . $this->user["salt"])) {//如果旧密码正确，则允许修改
                $data["paypwd"] = md5($newPass . $this->user["salt"]);
                $result = $this->user->isUpdate(true)->allowField(true)->save($data);
                if (false !== $result) {
                    $this->success();
                } else {
                    $this->error("支付密码修改失败");
                }
            } else {
                $this->error('原始支付密码错误');
            }
        } else {//无旧密码则设置
            $data["paypwd"] = md5($newPass . $this->user["salt"]);
            $result = $this->user->isUpdate(true)->allowField(true)->save($data);
            if (false !== $result) {
                $this->success("支付密码设置成功！");
            } else {
                $this->error("支付密码设置失败！");
            }
        }
    }

}
